package com.tracesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.commons.annotation.RuleValidate;
import com.tracesystem.controller.form.*;
import com.tracesystem.controller.info.IssueProjectInfo;
import com.tracesystem.controller.info.ProjectPageUserInfo;
import com.tracesystem.controller.info.SimpleProjectInfo;
import com.tracesystem.controller.info.SimpleUserInfo;
import com.tracesystem.model.InfoIssueProject;
import com.tracesystem.service.InfoIssueProjectService;
import com.tracesystem.util.WoniuCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/infoIssueProject")
@Api(tags = "项目操作相关内容")
public class InfoIssueProjectController {
    @Resource
    private InfoIssueProjectService projectService;

    /**
     * 新增项目
     *
     * @param newProjectForm
     * @return
     */
    @RuleValidate
    @ApiOperation("新增项目接口")
    @PostMapping("newproject")
    public Result<?> newProject(@RequestBody @Validated NewProjectForm newProjectForm){
        return projectService.addProject(newProjectForm);
    }

    /**
     * 关闭项目
     * @param closeProjectForm
     * @return
     */
    @RuleValidate
    @ApiOperation("关闭项目")
    @PostMapping("closeproject")
    public Result closeProject(@RequestBody @Validated CloseProjectForm closeProjectForm){
        return projectService.closeProject(closeProjectForm);
    }

    /**
     * 打开项目修改项目的状态
     * @param openProjectForm
     * @return
     */
    @RuleValidate
    @ApiOperation("打开项目")
    @PutMapping("openproject")
    public Result<?> openProject(@RequestBody @Validated OpenProjectForm openProjectForm){
        return projectService.enableProject(openProjectForm);
    }

    /**
     * 修改项目内容
     * @param changeProjectForm
     * @return
     */
    @ApiOperation("修改项目的内容：描述、信息")
    @PutMapping("changeproject")
    public Result<?> changeProject(@RequestBody ChangeProjectForm changeProjectForm){
       return projectService.changeProject(changeProjectForm);
    }
    /**
     * 分页模糊查询对象
     * @param projectPageForm
     * @return
     */
    @RuleValidate
    @ApiOperation("查询所分页查询所有项目")
    @GetMapping("getProjectPage")
    public Result<Page<IssueProjectInfo>> getAllProject(ProjectPageForm projectPageForm){
        return projectService.getProjectPageinfor(projectPageForm);

    }

    /**
     * 项目角色绑定
     *
     * @param projectBindRoleForm
     * @return
     */
    @RuleValidate
    @ApiOperation("项目绑定用户和角色")
    @PostMapping("projectBindRole")
    public Result<?> projectBindRole(@RequestBody @Validated ProjectBindRoleForm projectBindRoleForm) {

        return projectService.doProjectBindRole(projectBindRoleForm);
    }

    /**
     * 查询没有角色的用户
     *
     * @return
     */
    @RuleValidate
    @ApiOperation("查询可分配工作的员工")
    @GetMapping("getUsersByRoleIdNull")
    public Result<List<SimpleUserInfo>> getUsersByRoleIdNull() {
        return projectService.getListUserByRole();
    }

    /**
     * 移除成员
     * 修改用户的角色id为null表示为空闲角色
     *
     * @return
     */
    @RuleValidate
    @PostMapping("rmMember")
    @ApiOperation("移除项目中的成员")
    public Result<?> rmMember(@RequestBody @Validated RMMemberByUid rmMemberByUid) {
        return projectService.cutMember(rmMemberByUid);
    }

    /**
     * 分页查询项目的成员
     *
     * @param porjectPageUserForm
     * @return
     */
    @RuleValidate
    @ApiOperation("查询项目中的成员姓名、角色")
    @GetMapping("getPageProjectUsers")
    public Result<Page<ProjectPageUserInfo>> getPageProjectUsers(@Validated PorjectPageUserForm porjectPageUserForm) {
        return projectService.findPageProjectUsers(porjectPageUserForm);
    }

    /**
     * 查询所有项目的id和项目名称
     *
     * @return
     */
    @RuleValidate
    @ApiOperation("查询所有项目的id和项目名称")
    @GetMapping("getSimpleProject")
    public Result<List<SimpleProjectInfo>> getSimpleProject() {
        List<InfoIssueProject> list = projectService.list();
        List<SimpleProjectInfo> simpleProjectInfos = WoniuCopyUtil.copyMany(list, SimpleProjectInfo.class);
        return Result.getInstanceList(SimpleProjectInfo.class).setData(simpleProjectInfos);
    }


}

